package in.muton.edushare.pages.publicpages.deptpages;

import in.muton.edushare.EduSharePage;
import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.EduShareLogin;
import in.muton.edushare.pages.FooterLinks;
import in.muton.edushare.pages.publicpages.univpages.UnivBasePublic;
import in.muton.edushare.pages.publicpages.univpages.UnivPublic;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.url.URLHelper;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.util.URLUtils;
import com.edushare.common.vo.Departments;

public abstract class DeptBasePublic extends EduSharePage {
	private static final long serialVersionUID = 7973944157582612969L;

	public DeptBasePublic(PageParameters params) {
		super();
		getDepartment(params);
		addLinks(params);
		add(new Label("pageTitle", getDeptModel().getObject().getDeptName()));
		add(CSSPackageResource.getHeaderContribution(UnivBasePublic.class, "PublicPage.css"));
		add(CSSPackageResource.getHeaderContribution(UnivBasePublic.class, "Structure.css"));
		add(new Label("deptName", getDeptModel().getObject().getDeptName()));
		PageParameters params2 = new PageParameters();
		params2.add("0", new DeptDataHandler(getDeptModel()).getOwner()
				.getObject().getUniqueName());
		Link<Void> l = new BookmarkablePageLink<Void>("univLink",
				UnivPublic.class, params2);
		l.add(new Label("univName", new DeptDataHandler(getDeptModel())
				.getOwner().getObject().getUniversityName()));
		add(l);
		add(new FooterLinks("footerLinks"));
	}

	public void addLinks(PageParameters params) {
		for (String p : pageNames) {
			Link<Void> l = null;
			if (p.equals("DeptPublic")) {
				add(l = new BookmarkablePageLink<Void>(p,
						DeptPublic.class, params));
			} else if (p.equals("DeptCoursesPublic")) {
				add(l = new BookmarkablePageLink<Void>(p,
						DeptCoursesPublic.class, params));
			} else if (p.equals("DeptFacultyPublic")) {
				PageParameters params2 = new PageParameters();
				params2.put("0", URLUtils.escapeSplChars(getDeptModel().getObject().getDeptName()));
				params2.put("1", getDeptModel().getObject().getDepartmentId().toString());
				params2.put("2", "faculty");
				add(l = new BookmarkablePageLink<Void>(p,
						DeptPeoplePublic.class, params2));
			} else if (p.equals("DeptStudentsPublic")) {
				PageParameters params2 = new PageParameters();
				params2.put("0", URLUtils.escapeSplChars(getDeptModel().getObject().getDeptName()));
				params2.put("1", getDeptModel().getObject().getDepartmentId().toString());
				params2.put("2", "students");
				add(l = new BookmarkablePageLink<Void>(p,
						DeptPeoplePublic.class, params2));
			} else {
				if(EduShareSession.get().isLoggedIn()){
					add(l=new BookmarkablePageLink<Void>("login", URLLoader.getPageClass(URLConstants.USER_PROFILE_PATH)));
					l.add(new Label("myHome", "My Home"));
				}else{
					add(l = new BookmarkablePageLink<Void>("login",EduShareLogin.class));
					l.add(new Label("myHome", "Login"));
				}
			}
			if (p.equals(getPageName(params))) {
				l.add(new SimpleAttributeModifier("class", "pubMenuSel"));
			}
		}
	}

	public void getDepartment(PageParameters params) {
		int deptId = params.getInt("1", 0);
		if (deptId > 0) {
			Departments dept = new DepartmentsDAO(getSessionORMInterface())
					.getDepartmentByPk(deptId);
			if(dept != null){
				deptModel = new CayenneModel<Departments>(dept);
				return;
			}
		}
		throw new EduShareRuntimeException("Unable to find department");
	}

	public abstract String getPageName(PageParameters params);

	public IModel<Departments> getDeptModel() {
		return deptModel;
	}

	public static String[] pageNames = { "DeptPublic", "DeptCoursesPublic",
			"DeptFacultyPublic", "DeptStudentsPublic", "login" };

	private IModel<Departments> deptModel = null;

}
